package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 芦博
 * @description 制定方法添加定时任务
 * @createDate 2025/11/5 12:37
 */
@Component
public class ScheduleTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //开启定时任务
    @Scheduled(cron = "0/5 0 0 * * *") //每5秒执行一次
    public void customScheduleTask(){
        LambdaUpdateWrapper<LeaseAgreement> wrapper = new LambdaUpdateWrapper<>();
        Date nowTime = new Date();
        //添加已租约的用户
        wrapper.eq(LeaseAgreement::getStatus,LeaseStatus.SIGNED);
        //最后的租约日期 小于现在的时间 改为已过期
        System.out.println("开始执行定时任务...");
        wrapper.lt(LeaseAgreement::getLeaseEndDate, nowTime);
        wrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);;
        leaseAgreementService.update(wrapper);
        System.out.println("定时任务执行完毕...");
    }





}
